package com.kx.springmvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @ClassName UserController
 * @Author Sun
 * @Date 2021/5/20 17:14
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @RequestMapping("/reqandsession")
    public String reqAndSession(HttpServletRequest req, HttpSession session){
        req.setAttribute("name", "冰冰");
        session.setAttribute("address", "北京");
        return "/reqsession";
    }


    /**
     * 普通转发的情况下, model 会将数据放到 request 中
     * @param model
     * @return
     */
    @RequestMapping("/reqmodel")
    public String reqModel(Model model){
        model.addAttribute("name", "彭于晏");
        model.addAttribute("address", "上海");
        return "/reqsession";
    }


    /**
     * model  额外功能,如果是重定向会将我们放的数据通过请求参数的方式带出去,参数在浏览器的地址栏中
     * @param model
     * @return
     */
    @RequestMapping("/reqmodelredirect")
    public String reqModelRedirect(Model model){
        model.addAttribute("name", "aa");
        model.addAttribute("address", "bbb");
        return "redirect:/reqsession.jsp";
    }

    /**
     * ModelAndView
     * @param modelAndView
     * @return
     */
    @RequestMapping("/modelandview")
    public ModelAndView reqModelAndView(ModelAndView modelAndView){
        modelAndView.addObject("name", "娜扎");
        modelAndView.addObject("address","济南");
        modelAndView.setViewName("/reqsession");
        return modelAndView;
    }

}
